import Vue from 'vue'
import Router from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'
import Welcome from '../components/Welcome'
import Admin from '../components/authManage/Admin'
import AuthGroup from '../components/authManage/AuthGroup'
import AuthList from '../components/authManage/AuthList'
import ArticleList from '../components/article/ArticleList'
import Site from '../components/system/Site'

Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new Router({
  routes: [
    // 路由重定向，只要访问根路径直接跳转到/login页面
    { path: '/', redirect: '/login' },
    { path: '/login', component: Login },
    {
      path: '/home',
      component: Home,
      // redirect: '/welcome',
      children: [
        { path: '/Admin/Admin/welcome', component: Welcome },
        { path: '/Admin/Admin/admin', component: Admin },
        { path: '/Admin/AdminAuthGroup/authGroup', component: AuthGroup },
        { path: '/Admin/AdminAuthRule/authList', component: AuthList },
        { path: '/Admin/Article/articleList', component: ArticleList },
        { path: '/Admin/AdminConfig/site', component: Site }
      ]
    }
  ]
})

// 挂载路由导航守卫,to表示将要访问的路径，from表示从哪里来，next是下一个要做的操作 next('/login')强制跳转login
router.beforeEach((to, from, next) => {
  // 访问登录页，放行
  if (to.path === '/login') return next()
  // 获取token
  // const tokenStr = window.sessionStorage.getItem('token')
  // 没有token, 强制跳转到登录页
  // if (!tokenStr) return next('/login')
  next()
})

export default router
